<?php
// +----------------------------------------------------------------------
// | 随访系统
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2050 http://www.hedesoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------
// | Author: 小李子 < 490959289@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\controller\Base;
use think\Validate;
class System extends Base
{
    public function index()
    {
        $this->config();
    }
    //基础配置
    public function config()
    {
        $config = model('Config')->allConfigList();
        $this->assign('config',$config);
        return $this->fetch();
    }
    //新增配置
    public function newConfig()
    {
        $model_config = model('Config');
        $config = $_POST;
        $rule = [
            'key_info'  => 'require|max:30',
            'key_name'   => 'require|unique:config'
        ];

        $msg = [
            'key_info.require' => '字段描述必填',
            'key_info.max'     => '字段描述最多不能超过30个字符',
            'key_name.require'   => '字段名称必填',
            'key_name.unique'  => '字段名称已存在'
        ];

        $validate = new Validate($rule, $msg);
        if (!$validate->check($config)) {
            echo json_encode(['status'=>0,'msg'=>$validate->getError()]);exit();
        }

        $id=$model_config->insertGetId($config);
        if(empty($id)){
            echo json_encode(['status'=>0,'msg'=>'保存失败']);exit();
        }
        $this->create_adminlog('新增全局配置');
        echo json_encode(['status'=>1,'msg'=>'保存成功']);exit();
    }
    //保存配置
    public function saveConfig()
    {
        $model_config = model('Config');
        $config = $_POST;
        $data=array();
        foreach ($config['name'] as $key => $value) {
            if (empty($value)){
                echo json_encode(['status'=>0,'msg'=>'字段描述必填']);exit();
            }
            $data[$key]['id']=$key;
            $data[$key]['key_name']=$value;
            $data[$key]['key_value']=$config['value'][$key];

        }
        $model_config->saveAll($data);
        $this->create_adminlog('更新全局配置');
        echo json_encode(['status'=>1,'msg'=>'保存成功']);exit();
    }
    //删除配置
    public function deleteConfig()
    {
        $model_config = model('Config');
        $config_id = $_POST['id'];
        $model_config->where('id',$config_id)->delete();
        $this->create_adminlog('删除全局配置');
        echo json_encode(['status'=>1,'msg'=>'删除成功']);exit();
    }
	//日志列表
    public function adminlog()
    {
    	$where = array();
    	$q = $this->request->param('q');
    	if ($q){
    		 $where['content'] = array('LIKE', '%'.$q.'%');
    	}
    	$list = model('AdminLog')->where($where)->order('ctime desc')->paginate(15);
    	$page = $list->render();
    	$this->assign('list', $list);
    	$this->assign('page', $page);
    	return $this->fetch();
    }
    //短信配置
    public function sms()
    {
        $config = model('Config')->allConfig();
        $this->assign($config);
        return $this->fetch();
    }



}
